package com.bytedance.frameworks.core.a;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

public final class b extends Thread {
    private static List j;

    static {
        j = new ArrayList();
    }

    final Object a;
    private final AtomicBoolean h;
    int b;
    JSONObject c;
    volatile boolean d;
    int e;
    private LinkedList f;
    private e g;
    private long i;

    public b(LinkedList arg3, e arg4, AtomicBoolean arg5) {
        this.a = new Object();
        this.i = 0;
        this.b = 120;
        this.d = true;
        this.e = 100;
        this.f = arg3;
        this.g = arg4;
        this.h = arg5;
    }

    public static void a(b$a arg1) {
        if (!j.contains(arg1)) {
            j.add(arg1);
        }
    }

    private boolean a() {
        Object v0_2;
        boolean v0_1;
        LinkedList v2 = this.f;
        synchronized (v2) {
            if (this.f.isEmpty()) {
                v0_1 = false;
                return v0_1;
            }

            v0_2 = this.f.poll();
            if (v0_2 != null) {
                e v2_1 = this.g;
                if (v2_1 != null) {
                    try {
                        if (this.d) {
                            d v1 = this.g.c;
                            String v2_2 = ((com.bytedance.frameworks.core.a.a) v0_2).a;
                            v1.a(v2_2, ((com.bytedance.frameworks.core.a.a) v0_2).b);
                        } else {
                        }
                    } catch (Exception v0_3) {
                    }

                    v0_1 = true;
                } else {
                    label_15:
                    v0_1 = false;
                }

                return v0_1;
            }
            return false;
        }
    }

    public final void run() {
        while (!this.h.get()) {
            boolean v1 = this.a();
            this.g.a();
            if (this.h.get()) {
                return;
            }

            long v2 = System.currentTimeMillis();
            d v0 = this.g.c;
            if (v0.a() > (((long) this.e)) || (v2 - this.i) / 1000 > (((long) this.b))) {
                JSONObject v4 = new JSONObject();
                long v6 = v0.a(v4);
                if (v6 >= 0) {
                    this.i = v2;
                    if (v4.length() <= 0) {
//                        goto label_39;
                    } else {
                        try {
                            if (this.c != null) {
                                v4.put("header", this.c);
                            }
                            if (!this.d) {
                                v0.a(v6);
//                            goto label_39;
                            } else if (!android.support.design.a.l(v4.toString())) {
//                            goto label_39;
                            } else {
                                v0.a(v6);
                            }
                        } catch (Exception v0_1) {
                            Log.e("logSenderThred", "json resolve failed:" + Log.getStackTraceString(((Throwable) v0_1)));
                        }
                    }
                }
            }

//            label_39:
            if (j != null && !j.isEmpty()) {
                Iterator v0_2 = j.iterator();
                while (v0_2.hasNext()) {
                    v0_2.next();
                    if (com.bytedance.frameworks.core.a.b.b$a.b == null) {
                        continue;
                    }
                    com.bytedance.frameworks.core.a.b.b$a.b.i();
                }
            }

            if (v1) {
                continue;
            }

            Object v1_1 = this.a;
            synchronized (v1_1) {
                try {
                    this.a.wait(15000);
                    continue;
                } catch (InterruptedException e) {
                    Log.e("logSenderThred", "Thread wait failed :" + e.toString());
                    continue;
                }
            }
        }
    }

    public static final class b$a {
        static k b;
        public e a;

        public b$a(Context arg2, k arg3) {
            this.a = com.bytedance.frameworks.core.a.e.a(arg2.getApplicationContext());
            b = arg3;
            if (arg3 != null) {
                com.bytedance.frameworks.core.a.b.a(this);
            }
        }

        public final void a() {
            if (this.a != null) {
                this.a.b();
            }
        }

        public final void a(String arg2, String arg3) {
            if (this.a != null && !TextUtils.isEmpty(arg2) && !TextUtils.isEmpty(arg3)) {
                this.a.a(arg2, arg3);
            }
        }

        public final void a(String arg2, String arg3, float arg4) {
            if (this.a != null && !TextUtils.isEmpty(arg2) && !TextUtils.isEmpty(arg3)) {
                this.a.c(arg2, arg3, arg4);
            }
        }

        public final void a(boolean arg3) {
            if (this.a != null) {
                e v0 = this.a;
                if (v0.d != null) {
                    v0.d.d = arg3;
                }
            }
        }

        public final void b(String arg2, String arg3, float arg4) {
            if (this.a != null && !TextUtils.isEmpty(((CharSequence) arg2)) && !TextUtils.isEmpty(((CharSequence) arg3))) {
                this.a.a(arg2, arg3, arg4);
            }
        }

        public final void c(String arg2, String arg3, float arg4) {
            if (this.a != null && !TextUtils.isEmpty(((CharSequence) arg2)) && !TextUtils.isEmpty(((CharSequence) arg3))) {
                this.a.b(arg2, arg3, arg4);
            }
        }
    }
}

